#pragma once
#include <cstdarg>
#include <cstdio>
#include <ctime>
#include <cstring>

// LogMessage(Fatal, "listen(%d): %s", errno, strerror(errno));

enum LEVEL
{
    Debug,
    Nomal,
    Warning,
    Error,
    Fatal
};

const char *lvtable[] = {"Debug", "Normal", "Warning", "Error", "Fatal"};
const char *filepath = "./calclog.txt";

void LogMessage(LEVEL level, const char *format, ...)
{
#ifndef DEBUGSHOW
    if (level == Debug)
        return;
#endif
    char buffer[1024];
    time_t timestamp;
    time(&timestamp);
    tm *timeinfo = localtime(&timestamp);
    strftime(buffer, sizeof(buffer), "[%F %T]", timeinfo);
    snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), "%s: ", lvtable[level]);
    va_list ap;
    va_start(ap, format);
    vsnprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer), format, ap);
    va_end(ap);
    printf("%s\n", buffer);
    FILE *fp = fopen(filepath, "a");
    fprintf(fp, "%s\n", buffer);
    fclose(fp);
}